package com.huike.controller.system;

import com.huike.dto.SystemPageQueryDTO;
import com.huike.pojo.SysDept;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.system.DeptSystemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dept")
@Slf4j
public class DeptSystemContrpller {

    @Autowired
    private DeptSystemService deptSystemService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public Result list(SysDept dept) {
        log.info("获取部门列表：{}", dept);
        List<SysDept> depts = deptSystemService.selectDeptList(dept);
        return Result.success(depts);
    }

    /**
     * 部门管理-查询部门列表
     *
     * @param deptId
     * @return
     */
    @GetMapping("/list/exclude/{deptId}")
    public Result excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> depts = deptSystemService.selectDeptList(new SysDept());
        return Result.success(depts);
    }

    /**
     * 查询部门的详情（回显）
     *
     * @param deptId
     * @return
     */
    @GetMapping(value = "/{deptId}")
    public Result getInfo(@PathVariable Long deptId) {
        log.info("根据部门编号获取详细信息：{}", deptId);
        return Result.success(deptSystemService.selectDeptById(deptId));
    }

    /**
     * 根据部门编号删除
     *
     * @param deptId
     * @return
     */
    @DeleteMapping("/{deptId}")
    public Result remove(@PathVariable Long deptId) {
        log.info("根据部门编号删除：{}", deptId);
        deptSystemService.deleteDeptById(deptId);
        return Result.success();
    }

    /**
     * 部门列表
     *
     * @param dept
     * @return
     */
    @GetMapping("treeselect")
    public Result treeselect(SysDept dept) {
        log.info("部门列表显示：{}", dept);
        List<SysDept> depts = deptSystemService.treeselect(dept);
        return Result.success(deptSystemService.buildDeptTreeSelect(depts));
    }

    /**
     * 新增部门
     *
     * @param dept
     * @return
     */
    @PostMapping
    public Result add(@Validated @RequestBody SysDept dept) {
        log.info("新增部门:{}", dept);
        List<SysDept> depts = deptSystemService.addDept(dept);
        return Result.success(depts);
    }
}
